
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace NCS_CS
{
    public class CTP
    {
        /// <summary>
        /// 
        /// </summary>
        bool demo;
        System.IO.Ports.SerialPort SERIALPORT1;

        private System.Timers.Timer TIMER1;
        public event valueChangeEventHandler valueChange;
        public delegate void valueChangeEventHandler(double v);
        double old;
        public CTP()
        {
            try
            {
                this.SERIALPORT1 = new System.IO.Ports.SerialPort();
                this.SERIALPORT1.DtrEnable = true;
                this.SERIALPORT1.PortName = "COM1";
                this.SERIALPORT1.BaudRate = 1200;
                this.SERIALPORT1.DataBits = 7;
                this.SERIALPORT1.StopBits = System.IO.Ports.StopBits.One;
                this.SERIALPORT1.Handshake = System.IO.Ports.Handshake.XOnXOff;
                this.SERIALPORT1.Parity = System.IO.Ports.Parity.Odd;
                this.SERIALPORT1.ReadTimeout = 1000;
                this.SERIALPORT1.Open();
            }
            catch (System.UnauthorizedAccessException e)
            {
                modLog.savelogmsg(e.ToString());
            }
            catch (System.IO.IOException e)
            {
                modLog.savelogmsg(e.ToString());
            }

            TIMER1 = new System.Timers.Timer();
            TIMER1.Interval = 1000;
            TIMER1.Elapsed += this.Timer1_Tick;
        }
        public void startit()
        {
            TIMER1.Enabled = true;
        }
        public void stopit()
        {
            TIMER1.Enabled = false;
        }
        public double READ()
        {
            if (demo)
            {
                System.Random r = new Random();
                return r.NextDouble();
            }
            this.SERIALPORT1.DiscardInBuffer();
            string s = null;
            try
            {
                s = this.SERIALPORT1.ReadLine();
                s = this.SERIALPORT1.ReadLine();
                //return Conversion.Val(Strings.Right(s, Strings.Len(s) - 1));
                string s2 = s.Substring(0, s.Length - 1);
                double d1;
                System.Double.TryParse(s2, out d1);
                return d1;
            }
            catch (System.IO.IOException e)
            {
                Debug.Print(e.ToString());
                modLog.savelogmsg(e.ToString());
                return 0;
            }


        }
        //protected override void Finalize()
        //{
        //    this.SERIALPORT1.Close();
        //    base.Finalize();
        //}
        private void Timer1_Tick(System.Object sender, System.Timers.ElapsedEventArgs e) //Handles TIMER1.Elapsed
        {
            try
            {
                double n;
                n = READ();
                if (n != old)
                {
                    old = n;
                        valueChangeEventHandler vc = this.valueChange;
            if (vc != null)
            {
                vc(n);
            }

                }
            }
            catch (System.TimeoutException e1)
            {
                modLog.savelogmsg(e1.ToString());
            }
        }
        public void setTp(int id)
        {
            if (id == 0)
            {
                demo = true;
            }
            else if (id == 1)
            {
                demo = false;
                //setRECLEN(18);
            }
            else if (id == 2)
            {
                demo = false;
                //setRECLEN(12);
            }
        }
    }
}
